<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>行内水平居中</title>
  <style>
    #inline {
      text-align: center;
    }

    #block {
      width: 100px;
      height: 100px;
      background: #000;
      margin: 0 auto;
    }

    .container {
      text-align: center;
    }

    .container-block {
      width: 100px;
      height: 100px;
      background: #000;
      display: inline-block;
      margin: 10px;
    }

    .flex-container {
      /* 或者可以定义行内flex*/
      display: flex;
      justify-content: center;
    }

    .container2 {
      position: relative;
      width: 200px;
      height: 200px;
      background: #000;
    }

    .block2 {
      width: 100px;
      height: 100px;
      position: absolute;
      top: 50%;
      left: 50%;
      background: red;
      /*负边距 微调 数值是宽高的一半 */
      margin: -50px 0 0 -50px;
    }

    .container3 {
      position: relative;
      width: 200px;
      height: 200px;
      background: #000;
    }

    .block3 {
      width: 100px;
      height: 100px;
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background: red;
      /*负边距 微调 数值是宽高的一半 */
      margin: auto;
    }

    .container4 {

      width: 200px;
      height: 200px;
      background: #000;
      text-align: center;
      /* 实现inline元素垂直居中 */
      display: table-cell;
      vertical-align: middle;
    }

    .block4 {
      width: 100px;
      height: 100px;
      background: red;
      display: inline-block;

    }


    .container5 {

      width: 200px;
      height: 200px;
      background: #000;
      position: relative;
    }

    .block5 {
      width: 100px;
      height: 100px;
      background: red;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);

    }


    .container6 {

      width: 200px;
      height: 200px;
      background: #000;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .block6 {
      width: 100px;
      height: 100px;
      background: red;


    }
  </style>
</head>

<body>
  <p>行内元素水平居中</p>
  <div id="inline">center</div>
  <p>行内元素水平居中</p>
  <div id="block">center</div>

  <p>多个块状水平居中</p>
  <p>传统的方法是将要水平排列的块状元素设为display:inline-block，然后在父级元素上设置text-align:center，达到与上面的行内元素的水平居中一样的效果。</p>

  <div class="container">
    <div class="container-block">block1</div>
    <div class="container-block">block2</div>
    <div class="container-block">block4</div>
    <div class="container-block">block4</div>
  </div>

  <p>flex box实现水平居中</p>
  <div class="flex-container">
    <div class="container-block">block1</div>
    <div class="container-block">block2</div>
    <div class="container-block">block4</div>
    <div class="container-block">block4</div>
  </div>

  <p>已知宽高，水平垂直居中</p>
  <p>方法1：绝对定位和负边距</p>
  <div class="container2">
    <div class="block2"></div>
  </div>

  <p>不知宽高，水平垂直居中</p>
  <p>方法2：绝对定位和margin auto</p>
  <div class="container3">
    <div class="block3"></div>
  </div>


  <p>不知宽高，水平垂直居中</p>
  <p>如果要被居中的元素是inline或者inline-block时</p>
  <p>
    方法1：
    当要被居中的元素是inline或者inline-block的时候，可以巧妙的将父级容器设置为display:table-cell，配合text-align:center和vertical-align:middle即可以实现水平垂直居中。
  </p>
  <div class="container4">
    <div class="block4"></div>
  </div>

  <p>不知宽高，水平垂直居中</p>
  <p>
    方法2：
    定位 + transform
  </p>
  <div class="container5">
    <div class="block5"></div>
  </div>

  <p>不知宽高，水平垂直居中</p>
  <p>方法3：flex</p>
  <div class="container6">
    <div class="block6"></div>
  </div>


</body>

</html>